/**
 * @author yejinbo
 * @description '元素面板'
 */
import extend from 'lodash/extend'
const MINE = {
  cols: 1,
  rows: 1,
  idField: false,
  align: 'left',
  hidden: false,
  readonly: false
}
const dsText = {
  elname: 'ds-Text',
  isGroup: false,
  attrs: {
    text: 'default',
    color: '#bd1919'
  },
  mine: {
    describe: '',
    readonly: true,
    showType: 'TEXT'
  }
}
const Radio = {
  elname: 'el-radio',
  isGroup: false,
  attrs: {
    label: '',
    disabled: false,
    border: false,
    size: '',
    name: ''
  }
}
const RadioButton = {
  elname: 'el-radio-button',
  isGroup: false,
  attrs: {
    label: '',
    disabled: false,
    name: ''
  }
}
const RadioGroup = {
  elname: 'el-radio-group',
  isGroup: true,
  defaultNum: 3,
  child: ['el-radio', 'el-radio-button'],
  attrs: {
    size: '',
    disabled: false,
    'text-color': '#fff',
    fill: '#409EFF'
  },
  model: '',
  mine: {
    describe: '单选组',
    isneed: true,
    value: '',
    showType: 'RADIO',
    dataUrl: 'http://192.168.21.145:8088/rest/org/codeList/common_list/COMP_INDUSTRY?format=json&usertoken=admin'
  },
  list: [{label: '单选项1', value: 1}, {label: '单选项2', value: 2}, {label: '单选项3', value: 3}]
}
const ButtonGroup = {
  elname: 'el-button-group',
  isGroup: true,
  defaultNum: 2,
  child: 'el-button',
  attrs: {
    type: 'success'
  },
  model: '',
  mine: {
    describe: '按钮组',
    value: '',
    business: 'titleTool.buttons'
  },
  list: [{label: '按钮1', value: 1}, {label: '按钮2', value: 2}]
}
const DatePicker = {
  elname: 'el-date-picker',
  attrs: {
    type: ['datetime', 'datetimerange', 'daterange']
  },
  model: '',
  mine: {
    describe: '',
    isneed: false,
    showType: 'Date',
    value: ''
  }
}
const Input = {
  elname: 'el-input',
  isGroup: false,
  attrs: {
    type: ['text', 'textarea'],
    placeholder: ''
  },
  mine: {
    describe: 'test describe',
    isneed: true,
    showType: 'TEXT',
    value: ''
  }
}
const Button = {
  elname: 'el-button',
  attrs: {
    type: ['primary', 'default', 'success', 'danger', 'warning']
  },
  mine: {
    describe: '',
    isneed: false,
    text: 'demo'
  }
}
const checkBoxGroup = {
  elname: 'el-checkbox-group',
  isGroup: true,
  defaultNum: 3,
  child: ['el-checkbox', 'el-checkbox-button'],
  attrs: {
    disabled: false,
    'text-color': '#fff',
    fill: '#409eff'
  },
  mine: {
    describe: '',
    isneed: '',
    showType: 'CHECKBOX'
  },
  model: ['复选项1'],
  list: [{label: '复选项1', value: 1}, {label: '复选项2', value: 2}, {label: '复选项3', value: 3}]
}
const checkbox = {
  elname: 'el-checkbox',
  attrs: {
    label: '备选项',
    'true-label': '',
    'false-label': '',
    disabled: false,
    boder: false,
    size: '',
    name: '',
    checked: false,
    indeterminate: false
  },
  mine: {
    describe: '',
    isneed: false
  }
}
const InputNumber = {
  elname: 'el-input-number',
  model: 0,
  attrs: {
    size: 'large',
    step: 1,
    min: 1,
    disabled: false,
    name: '',
    label: 'demo'
  },
  mine: {
    describe: '',
    isneed: false,
    showType: 'TEXT'
  }
}
const Select = {
  elname: 'el-select',
  model: '',
  isGroup: true,
  attrs: {
    multiple: false,
    disabled: false,
    size: 'medium',
    clearable: false,
    name: '',
    placeholder: 'demo',
    filterable: true,
    remote: true,
    'popper-class': '',
    'no-data-text': 'no data',
    'no-match-text': 'no match data',
    'default-first-option': true
  },
  child: 'el-option',
  defaultNum: 3,
  mine: {
    describe: '下拉框',
    isneed: false,
    value: '',
    showType: 'SELECT',
    dataUrl: 'http://192.168.21.145:8088/rest/org/codeList/common_list/COMP_INDUSTRY?format=json&usertoken=admin'
  },
  list: [{value: '1', label: '备选项1'}, {value: '2', label: '备选项2'}, {value: '3', label: '备选项3'}]
}
const Option = {
  elname: 'el-option',
  attrs: {
    value: '',
    label: '',
    disabled: false
  }
}
const checkboxButton = {
  elname: 'el-checkbox-button',
  attrs: {
    label: '备选项',
    'true-label': '选中备选项',
    'false-label': '未选备选项',
    disabled: false,
    name: '',
    checked: false
  }
}
const TimePicker = {
  elname: 'el-time-picker',
  model: '',
  attrs: {
    'is-range': true,
    'range-separator': '至',
    'start-placeholder': '开始时间',
    'end-placeholder': '结束时间',
    placeholder: '选择时间范围',
    editable: true,
    size: 'medium ',
    'popper-class': '',
    name: ''
  },
  mine: {
    describe: '',
    isneed: false,
    showType: 'TIME',
    value: ''
  }
}
const Switch = {
  elname: 'el-switch',
  model: '',
  name: '',
  attrs: {
    'active-color': '#13ce66',
    'inactive-color': '#ff4949',
    'active-text': '',
    'inactive-text': ''
  },
  mine: {
    describe: '',
    isneed: false,
    showType: 'SWITCH',
    value: ''
  }
}
// const Silder = {
//   elname: 'el-slider',
//   model: 0,
//   attrs: {
//     min: 0,
//     max: 100,
//     disabled: false,
//     step: 1,
//     'show-input': false,
//     'show-input-controls': true,
//     'show-tooltip': true,
//     debounce: 1000
//   },
//   mine: {
//     describe: '',
//     isneed: false
//   }
// }
const Upload = {
  elname: 'el-upload',
  isUpload: true,
  child: ['el-button'],
  defaultNum: 1,
  attrs: {
    action: 'http://localhost:8080',
    multiple: true,
    data: {},
    name: '',
    drag: true,
    'list-type': ['text', 'picture', 'picture-card'],
    'file-list': [],
    limit: 20
  },
  mine: {
    describe: '',
    isneed: false,
    value: ''
  },
  list: [{label: '点此上传', value: 'upload'}]
}
const Rate = {
  elname: 'el-rate',
  model: null,
  attrs: {
    max: 5,
    disabled: false,
    'allow-half': false,
    'low-threshold': 2,
    'high-threshold': 4,
    colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
    'void-color': '#C6D1DE',
    'disabled-void-color': '#EFF2F7',
    'icon-classes': ['el-icon-star-on', 'el-icon-star-on', 'el-icon-star-on'],
    'void-icon-class': 'el-icon-star-off',
    'disabled-void-icon-class': 'el-icon-star-on',
    'show-text': false,
    'show-score': false,
    'text-color': '#1F2D3D',
    texts: ['极差', '失望', '一般', '满意', '惊喜'],
    'score-template': '{value}'
  },
  mine: {
    describe: '',
    isneed: false,
    value: ''
  }
}
const Getter = {
  'el-radio': {
    config: Radio,
    txt: '单选框',
    icon: '',
    unshow: true
  },
  'el-radio-button': {
    config: RadioButton,
    txt: '单选框',
    icon: '',
    unshow: true
  },
  'el-radio-group': {
    config: RadioGroup,
    txt: '单选框组',
    icon: ''
  },
  'el-button-group': {
    config: ButtonGroup,
    txt: '按钮组',
    icon: ''
  },
  'el-input': {
    config: Input,
    txt: '文本框',
    icon: ''
  },
  'el-date-picker': {
    config: DatePicker,
    txt: '日期选择器',
    icon: ''
  },
  'el-button': {
    config: Button,
    txt: '按钮',
    icon: ''
  },
  'el-checkbox-group': {
    config: checkBoxGroup,
    txt: '复选框组',
    icon: ''
  },
  'el-checkbox': {
    config: checkbox,
    txt: '复选框组',
    icon: '',
    unshow: true
  },
  'el-checkbox-button': {
    config: checkboxButton,
    txt: '复选框',
    icon: '',
    unshow: true
  },
  'el-input-number': {
    config: InputNumber,
    txt: '计数器',
    icon: ''
  },
  'el-select': {
    config: Select,
    txt: '下拉框',
    icon: ''
  },
  'el-option': {
    config: Option,
    txt: '下拉框项',
    icon: '',
    unshow: true
  },
  'el-switch': {
    config: Switch,
    txt: '开关选择',
    icon: ''
  },
  // 'el-slider': { //  滑块有bug
  //   config: Silder,
  //   txt: '滑块',
  //   icon: ''
  // },
  'el-time-picker': {
    config: TimePicker,
    txt: '时间选择器',
    icon: ''
  },
  'el-upload': {
    config: Upload,
    txt: '文件上传',
    icon: ''
  },
  'el-rate': {
    config: Rate,
    txt: '评分',
    icon: ''
  },
  'ds-text': {
    config: dsText,
    txt: '文本',
    icon: ''
  }
}
const List = {
  form: {
    getter: Getter,
    title: '表单元素'
  }
}
/**
 * @argument {blacklist: '黑名单(不进行显示的元素)'}
 */
export const getEls = (blacklist) => {
  let hasblacklist = blacklist && Array.isArray(blacklist)
  let ul = []
  for (let g in List) {
    let li = {}
    let arr = []
    let _gs = List[g].getter
    li['title'] = List[g].title
    for (let el in _gs) {
      if (_gs[el].unshow || (hasblacklist && blacklist.includes(el))) continue
      arr.push({
        value: el,
        txt: _gs[el].txt,
        icon: _gs[el].icon
      })
    }
    li['els'] = arr
    ul.push(li)
  }
  return ul
}
/**
 * @description `生成表单元素，允许传入配置项进行默认覆盖`
 */
export const allocate = config => {
  if (typeof config === 'string') {
    let _c = config
    config = {
      elname: _c
    }
  }
  if (!Getter[config.elname]) return
  let _config = Getter[config.elname].config
  if (!_config) return
  if (!config['attrs']) config['attrs'] = {}
  if (!config['mine']) config['mine'] = {}
  if (!_config['attrs']) _config['attrs'] = {}
  if (!_config['mine']) _config['mine'] = {}
  if (!_config['mine']['business']) _config['mine']['business'] = ''
  extend(_config.mine, MINE)
  let obj = JSON.parse(JSON.stringify(_config))
  if (_config.child) {
    obj.items = []
    let defChildName = Array.isArray(_config.child) ? _config.child[0] : _config.child
    let _itemConfig = Getter[defChildName].config
    if (!_itemConfig.mine) _itemConfig.mine = {}
    if (!_itemConfig['mine']['business']) _itemConfig['mine']['business'] = ''
    let keys = Object.keys(_itemConfig.attrs)
    if (!_config.attrs) return obj
    Object.keys(_config.attrs).forEach(key => {
      obj.attrs[key] = !config.attrs[key] ? (Array.isArray(_config.attrs[key]) ? (!_config.attrs[key][0] ? [] : _config.attrs[key][0]) : _config.attrs[key]) : config.attrs[key]
    })
    for (let i = 0; i < _config.list.length; i++) {
      let _item = JSON.parse(JSON.stringify(_itemConfig))
      for (let key of keys) {
        _item.attrs[key] = !config.attrs[key] ? (Array.isArray(_itemConfig.attrs[key]) ? _itemConfig.attrs[key][0] : _itemConfig.attrs[key]) : config.attrs[key]
        if (_config.child) {
          _item.attrs['label'] = _config.list[i].label
          _item.attrs['value'] = _config.list[i].value
        }
      }
      Object.keys(_item.mine).forEach(key => {
        _item.mine[key] = !_item.mine[key] ? (Array.isArray(_item.mine[key]) ? (!_item.mine[key][0] ? [] : _item.mine[key][0]) : _item.mine[key]) : _item.mine[key]
      })
      obj.items.push(_item)
    }
    if (!_config.mine) return obj
    Object.keys(_config.mine).forEach(key => {
      obj.mine[key] = !config.mine[key] ? (Array.isArray(_config.mine[key]) ? (!_config.mine[key][0] ? [] : _config.mine[key][0]) : _config.mine[key]) : config.mine[key]
    })
    return obj
  }
  Object.keys(_config).forEach(key => {
    switch (key) {
      case 'list':
        obj[key] = !config[key] ? _config[key] : config[key]
        break
      default:
        obj[key] = !config[key] ? (Array.isArray(_config[key]) ? (!_config[key][0] ? [] : _config[key][0]) : _config[key]) : config[key]
        break
    }
  })
  if (!_config.attrs) return obj
  Object.keys(_config.attrs).forEach(key => {
    obj.attrs[key] = !config.attrs[key] ? (Array.isArray(_config.attrs[key]) ? (!_config.attrs[key][0] ? [] : _config.attrs[key][0]) : _config.attrs[key]) : config.attrs[key]
    if (_config.elname === 'el-rate') {
      obj.attrs['colors'] = _config.attrs['colors']
      obj.attrs['texts'] = _config.attrs['texts']
      obj.attrs['iconClasses'] = _config.attrs['iconClasses']
    }
  })
  if (!_config.mine) return obj
  Object.keys(_config.mine).forEach(key => {
    obj.mine[key] = !config.mine[key] ? (Array.isArray(_config.mine[key]) ? (!_config.mine[key][0] ? [] : _config.mine[key][0]) : _config.mine[key]) : config.mine[key]
  })
  return obj
}
